 
layui.use('table', function(){
  var table = layui.table;
  
  
  //监听表格复选框选择
  table.on('checkbox(demo)', function(obj){
    console.log(obj)
  });
  //监听工具条
  table.on('tool(demo)', function(obj){
    var da = obj.data;
    //删除
    if(obj.event === 'del'){
      layer.confirm('确认删除吗?', function(index){
    	layer.close(index);
        obj.del();
        var url="../../listenRawstock/addOrUpdate.action";
  	 	var data={"isva":"2","rawstockId":da.rawstockId};
  	    $.post(url,data,function(mes){
  		  if(mes>0){
			layer.msg("删除成功");
  		  }
  	  },"json")
      });
    //修改
    } else if(obj.event === 'edit'){
    	layer.open({
  		  type: 2, //设置为iframe
  		  area: ['400px', '400px'],
  		  content: ['addOrUpdate.html', 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
  		  
  		  ,success: function(layero, index){
  			  
  			var url="../../listenRawstock/findRawstockId.action";
  	    	var data={"rawstockId":da.rawstockId};
  	    	setTimeout(() => {
  	    	$.post(url,data,function(mes){
  	    		var body = layer.getChildFrame('body', index);
				var iframe = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
				iframe.$("#rawstockId").val(mes.rawstockId);		 
				iframe.$("#rawstockName").val(mes.rawstockName);	
				iframe.$("#rtId").val(mes.rtId);	
				iframe.$("#rawstockPrice").val(mes.rawstockPrice);	
				iframe.$("#rawstockExplain").val(mes.rawstockExplain);	
				//调用子页面渲染的方法 刷新
				iframe.load();
  	    	},"json");
  	    	}, 300);
  		  }
  	  }); 
    }
  });
 

  
  //表单重载
  var $ = layui.$, active = {
		    reload: function(){
		      var rawstockName = $('#rawstockName');
		      var rtId = $('#rtId');
		      //执行重载    查询
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	rawstockName: rawstockName.val(),
		        	rtId: rtId.val()
		        }
		      });
		    }
		  };
  var url="../../listenRawstockType/findRawstockTypeRtId.action";
  var data=null;
  $.post(url,data,function(mes){
	  $.each(mes.data,function(i,item){
				  $("#rtId").append("<option value="+item.rtId+">"+item.rtName+"</option>")
			  load();
	  })
  },"json")
  $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
 
 
  
  /**
  * 增加
  */
  $('#insert').on('click', function(){
	  layer.open({
		  type: 2, //设置为iframe
		  area: ['450px', '450px'],
		  content: ['addOrUpdate.html', 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
 
	  }); 
	          
  });
});

function load(){
	var form = layui.form;
	form.render();
}
